'''
Created on 2012-9-12

@author: ltang
'''
import MySQLdb
import types
from MySqlDBHandle import MySqlDBHandle

class FollowersRelationship():
    mSQLInsertStatement = "INSERT INTO %s VALUES %s ON DUPLICATE KEY UPDATE LAST_UPDATE_TIMESTAMP = current_timestamp;"
    mSQLValueStatement = "(%s, %s, 1, current_timestamp, current_timestamp),"
    
    def __init__(self, iTableName, iUserId, iFollowerIdList):
        self.mTableName = iTableName
        self.mUserId = iUserId
        self.mFollowerIdList = iFollowerIdList
        
    def _sendToDB(self):
        
        print ("Insert Relationships into tabel = %s and UserId = %s ..." % (self.mTableName, self.mUserId))
        
        assert(type(self.mFollowerIdList) == types.ListType)
        lValueStatement =""
        for lFollowerId in self.mFollowerIdList:
            lValueStatement += (self.mSQLValueStatement % (self.mUserId, lFollowerId))
        lSQLStatement = self.mSQLInsertStatement % (self.mTableName, lValueStatement[:len(lValueStatement) - 1])
        
        try:
            MySqlDBHandle.execSqlStatement(lSQLStatement)
        except Exception, e:
            print "Error When Insert %s Followers relationship in '%s'" % (self.mUserId, self.mTableName)
        
        print ("Insert Relationships into tabel = %s and UserId = %s success!!!" % (self.mTableName, self.mUserId))
                
    
    def process(self):
        if type(self.mFollowerIdList) == types.ListType and len(self.mFollowerIdList) > 0:
            self._sendToDB()
    